﻿/*-------------------------------------------------------------------------
* 命名空间: SiliconSelect.Common/FileOperateHelper
* 类       名: FileOperateHelper
* 功      能:  文件操作类
* 时      间:  2023/7/29 14:33:28
* 版      本:  V1.0
* 作      者:  Fioman
* 格      言:  Talk is cheap,show me the code ^_^
*-------------------------------------------------------------------------*/
using DocumentFormat.OpenXml.Spreadsheet;
using SiliconSelect.Base;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Reflection.Metadata;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace SiliconSelect.Common
{
    public class FileOperateHelper
    {
        // 创建汇总目录
        public static void CreateSummaryDir()
        {
            // 1. 首先是检查时间和对应的日志文件是否存在,如果不存在就创建新目录,如果已经存在就不要创建新目录了. 8点以前按照昨天的日志目录进行.

            if (!File.Exists(GlobalValues.SummaryByDayDir))
            {
                Directory.CreateDirectory(GlobalValues.SummaryByDayDir);
            }


            TimeSpan nowDt = DateTime.Now.TimeOfDay;
            if (nowDt < DateTime.Parse("8:00").TimeOfDay)
            {
                GlobalValues.SummaryByDayPath = Path.Combine(GlobalValues.SummaryByDayDir, $@"KY02_{DateTime.Now.AddDays(-1).ToString("yyyyMMdd")}_summary.csv");
            }
            else
            {
                GlobalValues.SummaryByDayPath = Path.Combine(GlobalValues.SummaryByDayDir, $@"KY02_{DateTime.Now.ToString("yyyyMMdd")}_summary.csv");
            }
            if (!File.Exists(GlobalValues.SummaryByDayPath))
            {
                CreateAnEmptyFile(GlobalValues.SummaryByDayPath);
                CsvHelper.WriteToCsvSingleLine(GlobalValues.SummaryByDayPath, GlobalValues.SummaryTitle);
            }


            // 2. 判断汇总导出目录是否存在
            string summaryExportDir = Path.Combine(GlobalValues.SummaryExportDir, @$"{DateTime.Now.Month}月");
            if (!Directory.Exists(summaryExportDir))
            {
                Directory.CreateDirectory(summaryExportDir);
            }
            // 汇总导出的完整路径.
            GlobalValues.SummaryExportPath = summaryExportDir;

            // 2. 按晶棒编号汇总的目录,首先是创建日期文件.

            string summaryByCrystalNumberDir = Path.Combine(GlobalValues.SummaryByCrystalNumberDir, $@"{DateTimeHelper.GetDateTimeForSummary()}");
            if (!Directory.Exists(summaryByCrystalNumberDir))
            {
                Directory.CreateDirectory(summaryByCrystalNumberDir);

            }

            GlobalValues.SummaryByCrystalNumberPath = Path.Combine(summaryByCrystalNumberDir, $@"KY02_{GlobalValues.CrystalNumberCurrent!}.csv");
            if (!File.Exists(GlobalValues.SummaryByCrystalNumberPath))
            {
                CreateAnEmptyFile(GlobalValues.SummaryByCrystalNumberPath);
                CsvHelper.WriteToCsvSingleLine(GlobalValues.SummaryByCrystalNumberPath, GlobalValues.SummaryTitle);
            }


            // 3. 电阻率目录是否存在
            string resistivityDir = GlobalValues.ResistivityDataDir;
            if (!Directory.Exists(resistivityDir))
            {
                Directory.CreateDirectory(resistivityDir);
            }
            GlobalValues.ResistivityDataPath = Path.Combine(resistivityDir, $@"KY02_{GlobalValues.CrystalNumberCurrent!}.csv");
            if (!File.Exists(GlobalValues.ResistivityDataPath))
            {
                CreateAnEmptyFile(GlobalValues.ResistivityDataPath);
                CsvHelper.WriteToCsvSingleLine(GlobalValues.ResistivityDataPath, GlobalValues.ResistivityDataTitle);
            }
        }

        /// <summary>
        /// 根据文件路径创建一个空文件,如果文件已经存在则清空文件内容.
        /// </summary>
        /// <param name="filePath"></param>
        public static void CreateAnEmptyFile(string filePath)
        {
            try
            {
                if (File.Exists(filePath))
                {
                    File.SetAttributes(filePath, FileAttributes.Normal);
                }
                using (StreamWriter sw = new StreamWriter(filePath, false)) { }
                File.SetAttributes(filePath, FileAttributes.ReadOnly);
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(LogType.Warn, $"文件 <{filePath}> 创建失败,失败原因: " +
                    $"{ExceptionHelper.GetFullException(ex, MethodBase.GetCurrentMethod()!)}");
            }

        }

        /// <summary>
        /// 以追加的方式写入内容
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="content"></param>
        public static void WriteToFileAppend(string filePath, string content)
        {
            File.SetAttributes(filePath, FileAttributes.Normal);
            using (StreamWriter sw = new StreamWriter(filePath, true))
            {
                sw.WriteLine(content);
            }
            File.SetAttributes(filePath, FileAttributes.ReadOnly);
        }
    }
}
